#!/usr/bin/env python
#coding:utf-8
import tornado.gen
import tornado.web
from .baseHandler import BaseHandler
from bson import ObjectId
import time

class SearchHandler(BaseHandler):

    @tornado.gen.coroutine
    def get(self, keyword):
        self.set_header('Access-Control-Allow-Origin','http://cmf.com:8888')
        keyword = self.get_argument('keywords')
        isShort = self.get_argument('isShort')
        if keyword is None:
            results = [{'感冒','咳嗽'}]
        else:
            db = self.settings['db']
            #对keyword进行查询
            #首先匹配症状名
            insure_results = yield db.category.find({'$or':[{'main_cate':{'$regex':keyword}},{'detail_cate':{'$regex':keyword}}]}).to_list(5)
            #其次是对药品名进行检索，包括中文名，英文名，商品名，批准文号，拼音名，主治,功效
            drug_results = yield db.drug.find({'$or':[{'name':{'$regex':keyword}},
                                                {'py_name':{'$regex':keyword}},
                                                {'en_name':{'$regex':keyword}},
                                                {'product_name':{'$regex':keyword}},
                                                {'license_number':{'$regex':keyword}},
                                                {'major_cure':{'$regex':keyword}},
                                                {'effect':{'$regex':keyword}}]}).to_list(5)

            results = []
            if isShort:
                #遍历相关内容取出有用信息
                insure_short = []
                for short in insure_results:
                    temp = {}
                    temp['name'] = short['main_cate']
                    temp['target'] = str(short['_id'])
                    temp['index'] = 0
                    insure_short.append(temp)

                drug_short = []
                for drugInfo in drug_results:
                    temp_drug = {}
                    temp_drug['name'] = drugInfo['name']
                    temp_drug['target'] = str(drugInfo['_id'])
                    temp_drug['index'] = 0
                    drug_short.append(temp_drug)
                results = []
                results.append(insure_short)
                results.append(drug_short)
            else:
                results.append(self.find_formart(insure_results))
                results.append(self.find_formart(drug_results))
        self.response_msg(200,'ok',results)


    @tornado.gen.coroutine
    def post(self):
        self.set_header('Access-Control-Allow-Origin','http://cmf.com:8888')
        keyword = self.get_argument('keywords')
        isShort = self.get_argument('isShort')
        if keyword is None:
            results = [{'感冒','咳嗽'}]
        else:
            db = self.settings['db']
            #对keyword进行查询
            #首先匹配症状名
            insure_results = yield db.category.find({'$or':[{'main_cate':{'$regex':keyword}},{'detail_cate':{'$regex':keyword}}]}).to_list(5)
            #其次是对药品名进行检索，包括中文名，英文名，商品名，批准文号，拼音名，主治,功效
            drug_results = yield db.drug.find({'$or':[{'name':{'$regex':keyword}},
                                                {'py_name':{'$regex':keyword}},
                                                {'en_name':{'$regex':keyword}},
                                                {'product_name':{'$regex':keyword}},
                                                {'license_number':{'$regex':keyword}},
                                                {'major_cure':{'$regex':keyword}},
                                                {'effect':{'$regex':keyword}}]}).to_list(5)

            results = []
            if isShort:
                #遍历相关内容取出有用信息
                insure_short = []
                for short in insure_results:
                    temp = {}
                    temp['name'] = short['main_cate']
                    temp['target'] = str(short['_id'])
                    temp['index'] = 0
                    temp['type'] = 'cate'
                    insure_short.append(temp)

                drug_short = []
                for drugInfo in drug_results:
                    temp_drug = {}
                    temp_drug['name'] = drugInfo['name']
                    temp_drug['target'] = str(drugInfo['_id'])
                    temp_drug['index'] = 0
                    temp_drug['type'] = 'drug'
                    drug_short.append(temp_drug)
                results = []
                results.append(insure_short)
                results.append(drug_short)
            else:
                results.append(self.find_formart(insure_results))
                results.append(self.find_formart(drug_results))
        self.response_msg(200,'ok',results)